package com.ywb.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ywb.mapper.EmpExprMapper;
import com.ywb.mapper.EmpMapper;
import com.ywb.pojo.*;
import com.ywb.service.EmpService;
import com.ywb.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class EmpServiceImpl implements EmpService {

    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private EmpExprMapper empExprMapper;
// 原始
//    @Override
//    public PageResult<Emp> page(Integer page, Integer pageSize) {
////        调用mapper接口查询总记录数
//        Long total = empMapper.count();
////        mapper接口查询结果列表
//        List<Emp> rows = empMapper.list((page - 1) * pageSize, pageSize);
////        封装结果
//        return new PageResult<Emp>(total,rows);
//    }
//基于PageHelper查询
/*
* @Param page 页码
* @Param pageSize 每页记录数
* */

    @Override
    public PageResult<Emp> page(EmpQueryParam empQueryParam) {
        PageHelper.startPage(empQueryParam.getPage(),empQueryParam.getPageSize());
        List<Emp> empList = empMapper.list(empQueryParam);
        Page<Emp> p=(Page<Emp>) empList;
        return new PageResult<>(p.getTotal(),p.getResult());
    }

    @Transactional(rollbackFor = Exception.class)//事务管理
    @Override
    public void save(Emp emp) {
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.insert(emp);
        List<EmpExpr> exprList=emp.getExprList();
        if(!CollectionUtils.isEmpty(exprList)){
            exprList.forEach(empExpr -> {
                empExpr.setEmpId(emp.getId());
            });
            empExprMapper.insertBatch(exprList);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<Integer> ids) {

        empMapper.deleteByIds(ids);
        empExprMapper.deleteByEmpIds(ids);
    }

    @Override
    public Emp getinfo(Integer id) {
        return empMapper.getById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(Emp emp) {
//        1.修改员工基本信息
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.updateById(emp);
//        2.修改员工对应的职位信息\
//        先删再加
        empExprMapper.deleteByEmpIds(Arrays.asList(emp.getId()));
        List<EmpExpr> exprList=emp.getExprList();
        if(!CollectionUtils.isEmpty(exprList))
        {
            exprList.forEach(empExpr -> {empExpr.setEmpId(emp.getId());});
            empExprMapper.insertBatch(exprList);
        }
    }

    @Override
    public LoginInfo login(Emp emp) {
        Emp e=empMapper.getByUsernameAndPassword(emp);
        if (e!=null)
        {
            log.info("员工登录成功,员工信息:{}",e);
            Map<String,Object> clamis = new HashMap<>();
            clamis.put("id",e.getId());
            clamis.put("username",e.getUsername());
            String JWT = JwtUtils.generateToken(clamis);
            return new LoginInfo(e.getId(),e.getUsername(),e.getName(),JWT);
        }
        return null;

    }
}
